/*
   Copyright 2008, Andres Postiglioni

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package com.aconcagua.beans {
  import com.aconcagua.factory.BeanFactory;

  import mx.core.IMXMLObject;

  /**
   * @author Andres Postiglioni
   */
  [DefaultProperty("metadata")]
  public class Bean implements BeanDefinitionFactory, IMXMLObject {
    private var _id: String;
    private var _type: Class;
    private var _lazy: Boolean;
    private var _scope: String;
    private var _metadata: Array;

    public function Bean() {
      _scope = BeanDefinitionSupport.SCOPE_SINGLETON;
    }

    public function get id(): String {
      return _id;
    }

    public function get type(): Class {
      return _type;
    }

    public function set type(aType: Class): void {
      _type = aType;
    }

    public function get lazy(): Boolean {
      return _lazy;
    }

    public function set lazy(isLazy: Boolean): void {
      _lazy = isLazy;
    }

    public function get scope(): String {
      return _scope;
    }

    [Inspectable(enumeration="singleton,prototype", defaultValue="singleton")]
    public function set scope(aScope: String): void {
      _scope = aScope;
    }

    public function set metadata(meta: Array): void {
      _metadata = meta;
    }

    public function initialized(document:Object, id:String): void {
      _id = id;
    }

    public function isRegisteredIn(beanFactory: BeanFactory): Boolean {
      return beanFactory.containsBean(_id);
    }

    public function registerIn(beanFactory: BeanFactory): void {
      var definition: BeanDefinitionSupport = new BeanDefinitionSupport(_type, _id);

      definition.scope = _scope;
      definition.lazy = _lazy;
      definition.metadata = _metadata;

      beanFactory.registerBeanDefinition(definition);
    }

    public function retrieveFrom(beanFactory: BeanFactory): Object {
      return beanFactory.getBean(_id);
    }
  }
}